#include <iostream>
#include <cstring>
#include <algorithm>
#include <cstdio>
#include <cmath>
#include <vector>
#include <list>
#include <queue>
#include <stack>
#include <map>
#include <unordered_map>
#include <unordered_set>
#include <set>
#include <bitset>
#include <utility>
using namespace std;

#define mm(a, n) memset(a, n, sizeof a)
#define mk(a, b) make_pair(a, b)

const double eps = 1e-6;
const int INF = 0x3f3f3f3f;

typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int> PII;
typedef pair<double, double> PDD;
typedef pair<LL, LL> PLL;
typedef pair<int, LL> PIL;

inline void quickread() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
}
const int N = 1e6;

struct Node {
    int data, next;
}node[N];

int head, n;

inline void solution() {
    cin >> head >> n;
    unordered_set<int> st;
    for (int i = 0; i < n; i ++ ) {
        int idx;
        cin >> idx;
        cin >> node[idx].data >> node[idx].next;
    }
    vector<PII> res, del;
    int p = head;
    while (p != -1) {
        int data = node[p].data;
        if (st.find(abs(data)) == st.end())
        {
            res.push_back(mk(p, data));
            st.insert(abs(data));
        } else {
            del.push_back(mk(p, data));
        }
        p = node[p].next;
    }

    printf("%05d %d %05d\n", res[0].first, res[0].second, res[1].first);
    for (int i = 1; i < res.size() - 1; i ++ ) 
        printf("%05d %d %05d\n", res[i].first, res[i].second, res[i + 1].first);
    printf("%05d %d -1\n", res.back().first, res.back().second);

    printf("%05d %d %05d\n", del[0].first, del[0].second, del[1].first);
    for (int i = 1; i < del.size() - 1; i ++ ) 
        printf("%05d %d %05d\n", del[i].first, del[i].second, del[i + 1].first);
    printf("%05d %d -1\n", del.back().first, del.back().second);
}

int main() {
    freopen("input.txt", "r", stdin);
    quickread();
    solution();
    return 0;
}